package com.yaorange.jk.web.baseinfo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.yaorange.jk.entity.Factory;
import com.yaorange.jk.service.FactoryService;
import com.yaorange.jk.utils.Pagination;
import com.yaorange.jk.web.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.List;


@Controller("factoryAction")
@Scope("prototype")
public class FactoryAction extends BaseAction implements ModelDriven<Factory>  {

    @Autowired
    private FactoryService factoryService;

    private Pagination page=new Pagination();

    private List<Factory> factoryList;

    private Factory model= new Factory();

    public String list(){
        Pagination pagination =  factoryService.findPage(page);
        pagination.setUrl("factoryAction_list");
        ActionContext.getContext().getValueStack().push(pagination);
        return "list";
    }

    public String tocreate(){
        return "tocreate";
    }

    public String insert(){
        factoryService.save(model);
        return list();
    }

    public String toupdate(){
    Factory factory = factoryService.findById(model.getId());
    super.push(factory);

    factoryList = factoryService.findAll();
    factoryList.remove(factory);
    return "toupdate";
    }

    public String update(){
    factoryService.update(model);
    return list();
    }

    /**
    * @return
    */
    public String delete(){
        String[] ids = model.getId().split(", ");
        for (String id:ids) {
            factoryService.deleteById(model.getId());
        }
         return list();
    }

    public String toview()
    {
    Factory factory = factoryService.findById(model.getId());
    push(factory);
    return "toview";
    }
    public void setPage(Pagination page) {
    this.page = page;
    }

    public List<Factory> getFactoryList() {
    return factoryList;
    }

    @Override
    public Factory getModel() {
    return model;
    }
}
